/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sunrise.bean;

import com.sunrise.entity.Event;
import com.sunrise.entity.User;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleModel;

@ManagedBean
@ViewScoped
public class PublicCalendarBean extends CalendarBean {

    private User publicUser;

    public User getPublicUser() {
        return publicUser;
    }

    public void setPublicUser(User publicUser) {
        this.publicUser = publicUser;
    }

    @Override
    public void init() {
    }

    @PostConstruct
    public void populateCalendar() {
        publicUser = (User) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("searchedUser");

        if (publicUser != null) {

            eventManager.setUser(publicUser);
            List<Event> events = eventManager.findUserEvents();

            scheduleModel = new DefaultScheduleModel();

            for (Event e : events) {
                e.setLoadedByOwner(false);
                e.setLoadedInPublicCalendar(true);
                scheduleModel.addEvent(e);
            }
        }
    }

    @Override
    public void onEventSelect(SelectEvent selectEvent) {
        event = (Event) selectEvent.getObject();

        if (!event.isPublicEvent()) {
            return;
        }

        super.onEventSelect(selectEvent);
        RequestContext context = RequestContext.getCurrentInstance();
        context.execute("PF('eventDialog').show();");
    }

    @Override
    public void onDateSelect(SelectEvent selectEvent) {
        super.onDateSelect(selectEvent); //To change body of generated methods, choose Tools | Templates.
    }

}
